/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.bsplus.dao.jpa;

import java.util.List;

import org.bsplus.dao.UserDAO;
import org.bsplus.domain.User;

/**
 * 
 * @author sam
 */
public class JpaUserDAO extends JpaDAO implements UserDAO
{

	@Override
	public User getUser(Long id)
	{
		return getJpaTemplate().find(User.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public User getUser(String username)
	{
		List<User> results = getJpaTemplate().find(
				"from User where username=?", username);
		if (results.isEmpty())
		{
			return null;
		}
		else if (results.size() == 1)
		{
			return results.get(0);
		}
		else
		{
			throw new IllegalStateException("> 1 result found");
		}
	}

	@Override
	public List<User> getAllUsers()
	{
		return findAll(User.class);
	}

	@Override
	public void saveOrUpdateUser(User user)
	{
		getJpaTemplate().persist(user);
	}

}
